package com.jiangfan.tank;

import java.awt.*;
import java.util.Random;
import java.awt.Rectangle;

public class Tank {
    private int x, y;
    private Dir dir = Dir.DOWN;

    private static final int SPEED = 3;

    private boolean moving = true;
    private Random random = new Random();
    private TankFrame tf = null;
    private boolean live = true;
    private Group group = Group.Enemy;

    public static int WIDTH = ResourceMgr.goodTankU.getWidth();
    public static int HEIGHT = ResourceMgr.goodTankU.getHeight();

    Rectangle rect = new Rectangle();

    public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
        super();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tf = tf;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }

    private void randomDir() {

        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void paint(Graphics g) {
        if(!live) tf.enemyTanks.remove(this);

        switch(dir) {
            case LEFT:
                g.drawImage(this.group == Group.Ally ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.Ally ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.Ally ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.Ally ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null);
                break;
        }
        move();
    }


    private void move() {
        if(!moving) return;

        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }

        if(this.group == Group.Enemy && random.nextInt(100) > 95)
            this.fire();

        if(this.group == Group.Enemy && random.nextInt(100) > 95)
            randomDir();

        boundsCheck();

        //update rect
        rect.x = this.x;
        rect.y = this.y;
    }


    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    /**
     * 检测坦克是否越界
     */
    private void boundsCheck() {
        if (this.x < 0)
            x = 0;   // 0 是窗口的左边
        if (this.y < 30)
            y = 30; // 上面，30 是标题栏的高度
        if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH -2)
            x = TankFrame.GAME_WIDTH - Tank.WIDTH -2; // 右边
        if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT -2 )
            y = TankFrame.GAME_HEIGHT -Tank.HEIGHT -2; // 下面
    }

    public void fire(){
        int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
        int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;

        tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));

        if(this.group == Group.Ally) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
    }

    public void die() {
        this.live = false;
    }



}
